package de.muahd.saga.ui.vaadin.config;

import org.dellroad.stuff.vaadin.ContextApplication;
import org.dellroad.stuff.vaadin.VaadinConfigurable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

import de.muahd.saga.common.model.Customer;
import de.muahd.saga.common.model.Customer_;
import de.muahd.saga.services.core.api.CustomerService;
import de.muahd.saga.ui.vaadin.views.components.menu.Menu;
import de.muahd.saga.ui.vaadin.views.layouts.ApplicationLayoutWindow;

/**
 * @author user
 *
 */
@Component
@VaadinConfigurable
@Scope("prototype")
public class VaadinApplication extends  ContextApplication {

	private static final long serialVersionUID = 1L;

	private Window window;

	@Autowired
	private Menu menu;

	@Autowired
	private CustomerService customerService;


	private Table createCustomerTable(final Label selected) {
		final Table table = new Table("ISO-3166 Country Codes and flags");
		table.setImmediate(true);
		// Icons for column headers
		table.setContainerDataSource(new BeanItemContainer(Customer.class, customerService.findAll()));
		table.setVisibleColumns(new String[] { Customer_.id.getName(), Customer_.firstname.getName() });
		table.setSelectable(true);

		table.addListener(new Table.ValueChangeListener() {
			@Override
			public void valueChange(ValueChangeEvent event) {
				// in multiselect mode, a Set of itemIds is returned,
				// in singleselect mode the itemId is returned directly
				Customer value = (Customer) event.getProperty().getValue();
				if (null == value) {
					selected.setValue("No selection");
				} else {
					selected.setValue("Selected: " + value.getFirstname());
				}
			}
		});

		return table;
	}

	@Override
	protected void initApplication() {
		window = new ApplicationLayoutWindow();
		setMainWindow(window);

		VerticalLayout left = new VerticalLayout();
		left.addComponent(menu);
		left.setSizeFull();
		window.addComponent(left);

		Customer customer = new Customer();
		customer.setFirstname("Marc");
		customer.setLastname("Giffing");
		customerService.saveCustomer(customer);

		// vertically divide the right area
		HorizontalLayout horizontalLayout = new HorizontalLayout();
		horizontalLayout.setSizeFull();
		Button button = new Button("Button");
		horizontalLayout.addComponent(button);
		horizontalLayout.setComponentAlignment(button, Alignment.TOP_LEFT);
		left.addComponent(horizontalLayout);
		final Label selected = new Label("No selection");
		Table table = createCustomerTable(selected);
		left.addComponent(table);


		window.addComponent(selected);

	}


}